#ifndef router_factory_h__
#define router_factory_h__

#include "common.h"
#include <type_traits>
#include "router.h"

struct router_factory
{
	static router_factory& instance()
	{
		static auto _instance = new router_factory;
		return *_instance;
	}
	router_family* get_router_family(const char* app)
	{
		auto itor = _router_map.find(app);
		return itor != _router_map.end() ? itor->second : nullptr;
	}
	bool register_router(router_family* router)
	{
		auto ret = _router_map.insert(std::make_pair(router->_family, router));
		return ret.second;
	}
private:
	std::map<std::string, router_family*> _router_map;
};

template<typename T>
struct app_router
{
	static_assert(std::is_convertible<T*, router_family*>::value, "class T must inherit from router_family");
	app_router()
	{
		router_factory::instance().register_router(new T);
	}
};

#define REGISTER_APP_ROUTER(T) static app_router<T> app_##T;
#endif // router_factory_h__
